package cn.tedu._02notice.controller;

import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import cn.tedu._02notice.pojo.vo.NotiecDetailInfoVO;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notice/")
public class NoticeContriller {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    public String addNotice(NoticeAddParam noticeAddParam){
        System.out.println("添加咨询" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return "新增咨询成功";
    }

    @GetMapping("list")
    public List<NoticeListVO> listNotice(NoticeListQuery noticeListQuery){
        System.out.println("查询条件：" + noticeListQuery);
        List<NoticeListVO> selectNoticeList = noticeMapper.selectNoticeList(noticeListQuery);
        return selectNoticeList;
    }

    @GetMapping("detail")
    public NotiecDetailInfoVO selectNoticeById(Long id){
        NotiecDetailInfoVO selectNoticeById = noticeMapper.selectNoticeById(id);
        return selectNoticeById;
    }

    @DeleteMapping("del")
    public String delNotice(Long id){
        System.out.println("删除咨询" + id);
        int result = noticeMapper.detNoticeById(id);
        if (result > 0){
            return "删除咨询成功";
        }
        return "删除咨询失败";
    }

    @PostMapping("update")
    public String updateNotice(NoticeUpdateParam noticeUpdateParam){
        System.out.println("修改咨询" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int result = noticeMapper.updateNoticeById(notice);
        if (result > 0){
            return "修改咨询成功";
        }
        return "修改咨询失败";
    }

}
